﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace fsw_backup
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        string _sourcePath = null;
        string _backupPath = null;

        Dictionary<string, EventItem> _pendingEvents = new Dictionary<string, EventItem>();

        private void Log(string text)
        {
            lstLog.Items.Insert(0, string.Format("{0}: {1}", DateTime.Now, text));

            if (lstLog.Items.Count > 200)
                lstLog.Items.RemoveAt(lstLog.Items.Count - 1);
        }

        private void GotChange(string FullPath)
        {
            if (!_pendingEvents.ContainsKey(FullPath))
            {
                EventItem item = new EventItem(FullPath);
                _pendingEvents.Add(FullPath, item);
            }
            else
            {
                EventItem item = _pendingEvents[FullPath];
                item.AddEvent();
            }
        }

        private void btnSelectSource_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = "Select folder you want to backup";
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                return;

            _sourcePath = fbd.SelectedPath;
            fileSystemWatcher1.Path = _sourcePath;
        }

        private void btnSelectBackup_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = "Select your backup folder";
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                return;

            _backupPath = fbd.SelectedPath;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(string.Format("_sourcePath = {0}", _sourcePath));
            sb.AppendLine(string.Format("_backupPath = {0}", _backupPath));
            lblStatus.Text = sb.ToString();

            foreach (EventItem item in _pendingEvents.Values.ToList())
            {
                if (item.ReadyToLog)
                {
                    Log("Event : " + item.ToString());
                    item.SetNextEventLog();
                }
                if (item.ReadyToBackup)
                {
                    if (_backupPath == null)
                        Log("BackupNull : " + System.IO.Path.GetFileName(item.FullPath));
                    else
                        try
                        {
                            BackupHelper.BackupFile(item.FullPath, _backupPath);
                            Log("BackupOk : " + System.IO.Path.GetFileName(item.FullPath));
                        }
                        catch (Exception ex)
                        {
                            Log("BackupFailed : " + System.IO.Path.GetFileName(item.FullPath));
                        }

                    _pendingEvents.Remove(item.FullPath);
                }
            }
        }

        private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
        {
            GotChange(e.FullPath);
        }

        private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            GotChange(e.FullPath);
        }
    }
}
